import argparse

parser = argparse.ArgumentParser(description="""程序描述""")
parser.add_argument("-dir", type=str, default=None, help="运行参数")
parser.add_argument("-output", type=str, default=None, help='输出目录')
parser.add_argument("-image_dirname", type=str, default="static/img/", help="运行参数")
args = parser.parse_args()

import logging
import os.path

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)

filehandler = logging.FileHandler(os.path.basename(__file__) + ".log")
filehandler.setLevel(logging.INFO)
filehandler.setFormatter(formatter)
logger.addHandler(filehandler)
if not os.path.exists(args.output):
    os.makedirs(args.output)

os.chdir(args.output)
from utils.ufile import *

uf = ufile(args.dir)
for mf in uf.get_markdown_files():
    # the dir for saving image

    file_name = uf.get_file_marker(mf)
    relative_path=os.path.dirname(str(mf).replace(args.dir,""))[1:]

    file_save_path=os.path.join(args.output,relative_path, file_name)

    local_markdown_content = umarkdown(mf,args.image_dirname).get_local_markdown()

    if not os.path.exists(os.path.join(args.output,relative_path)):
        os.makedirs(os.path.join(args.output,relative_path))

    with open(file_save_path, "w") as f:
        f.write(local_markdown_content)
